const puppeteer = require('puppeteer');
const { mn } = require('./congig/default');
const srcToImg = require('./helper/srcToimg');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    // 目标网站
    await page.goto('https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%BC%E5%B5%B0%CD%F8%B9%D9%CD%F8%C3%C3%D7%D3%CD%BC&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111');
    console.log('go to success');

    // 改变网站高度，这样我们就可以不用滚动，而获取更多的图片
    await page.setViewport({
        width: 1920,
        height: 4080
    });
    console.log('reset viewport');

    // await page.focus('#su');
    await page.keyboard.sendCharacter('煎蛋网官网妹子图');
    await page.click('.s_btn');
    console.log('go to search list');

    page.on('load', async () => {
        console.log('page loading done, start fetch...');

        const srcs = await page.evaluate(() => {
            const images = document.querySelectorAll('img.main_img');
            return Array.prototype.map.call(images, img => img.src);
        });
        console.log(`get ${srcs.length} images, start, download`);

        srcs.forEach(async (src) => {
            // sleep
            await page.waitFor(200);
            await srcToImg(src, mn);
        });

        await browser.close();

    });
})();